package com.liu.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.eduService.entity.EduChapter;
import com.liu.eduService.entity.EduVideo;
import com.liu.eduService.entity.chapter.ChapterVo;
import com.liu.eduService.entity.chapter.VideoVo;
import com.liu.eduService.mapper.EduChapterMapper;
import com.liu.eduService.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.eduService.service.EduVideoService;
import com.liu.serviceBase.exception.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author LSJ
 * @since 2022-01-07
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;
    @Override
    public List<ChapterVo> findChapterBycourseId(String courseId) {
        ArrayList<ChapterVo> chapterVoArrayList = new ArrayList<>(); // 最终结果

//        1\ 获取到 章节list
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(wrapper);

//        2  获取课时信息
        QueryWrapper<EduVideo> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("course_id",courseId);
        List<EduVideo> videoList = eduVideoService.list(wrapper2);

        // 3  //填充章节vo数据
        for (int i = 0; i < chapterList.size(); i++) {
            EduChapter eduChapter = chapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            chapterVoArrayList.add(chapterVo);
            ArrayList<VideoVo> videoVoArrayList = new ArrayList<>();

            for (int j = 0; j <videoList.size(); j++) {
                EduVideo eduVideo = videoList.get(j);

                // 判断 章节id 与 小节的父节点id 是否一致
                String chapterId = eduVideo.getChapterId();
                String eduChapterId = eduChapter.getId();

                if (Objects.equals(chapterId,eduChapterId)){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
//                    一致，将小节加入到课程中
                    videoVoArrayList.add(videoVo);
                }

            }
                chapterVo.setChildren(videoVoArrayList);
        }


        return chapterVoArrayList;
    }

    @Override
    public boolean removeByChapterId(String chapterId) {

//  根据id查询是否存在视频，如果有则提示用户尚有子节点
     boolean ExistVideo =    eduVideoService.getCountByChapterId(chapterId);
     if (ExistVideo){
         throw new MyException(20001,"该章节存在若干小节，不可删除！");
     }

        int delete = baseMapper.deleteById(chapterId);
        return  delete > 0 ;
    }

    @Override
    public boolean deletechapterBycourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        int delete = baseMapper.delete(wrapper);
        return delete>0;

    }
}
